package com.sxj.corejava.code13_线程;

import java.util.Scanner;

/**
 * @author 石小俊
 */
public class Test04_生命周期 {
    public static void main(String[] args) {
        OtherThread ot = new OtherThread();
        Thread t1 = new Thread(ot, "线程1");
        t1.start();

        // main线程
        for (int i = 0; i < 100; i++) {
//            if(i == 10){
//                try {
//                    t1.join();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
            System.out.println(Thread.currentThread().getName() + "正在打印数字:" + i);
        }

        // 主线程结束之后立刻中断t1的休眠
        // 此时t1立刻结束休眠状态，进入就绪状态
        t1.interrupt();
        // 此时会出现异常:java.lang.InterruptedException: sleep interrupted
        // 表示休眠非正常结束

    }
}

class OtherThread implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i == 10) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                Scanner sc = new Scanner(System.in);
//                System.out.println("请输入:");
//                String s = sc.next();
            }

            System.out.println(Thread.currentThread().getName() + "正在打印数字:" + i);
        }
    }
}
